package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.UserRole;
import com.sjlh.centurylianhua.mapper.RoleMapper;
import com.sjlh.centurylianhua.mapper.UserRoleMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/userrole")
@CrossOrigin
public class UserRoleController {
    @Resource
    private UserRoleMapper urm;
    @Resource
    private RoleMapper role;


    @RequestMapping("/tree")
    public Object tree(@RequestBody SearchCon sc){
        QueryWrapper<UserRole> wra = new QueryWrapper<UserRole>();
        wra.like(StringUtils.isnotNull(sc.getName()), "RoleName", sc.getName());
        wra.apply("u.id = ur.userId and r.roleId=ur.roleId");
        // 封装分页对象
        if(sc.getTypeId()!=null) {
            wra.eq("ur.userId",sc.getTypeId());
            return new ResultData(true,20000," " ,urm.urlist1(wra),null);
        }else {
            if(sc.getPage()!=null) {
                Page<UserRole> page = new Page<UserRole>(sc.getPage(), sc.getLimit());
                List<UserRole> rolePage = urm.urlist2(page, wra);
                return new ResultData(true,20000," " ,rolePage,page.getTotal());
            }else{
                Long count= Long.valueOf(urm.urlist1(wra).size());
                return new ResultData(true,20000," " ,urm.urlist1(wra),count);
            }
        }
    }
    @RequestMapping("/ins")
    public Object ins(@RequestBody SearchCon sc) {
        List<UserRole> list=new ArrayList<UserRole>();
        QueryWrapper<UserRole> wra = new QueryWrapper<UserRole>();
        wra.eq(sc.getTypeName()!=null, "userId", sc.getTypeName());
        List<UserRole> listuser= urm.selectList(wra);
        if(sc.getNamelist()!=null && sc.getNamelist().length!=0 && listuser != null && listuser.size() != 0){
            UpdateWrapper<UserRole> updateWrapper = new UpdateWrapper<UserRole>();//先清除掉他有的商品
            updateWrapper.eq(sc.getTypeName() != null, "userId", sc.getTypeName());
            urm.delete(updateWrapper);//移除所有商品
        }

        boolean r = true;
        for (String s : sc.getNamelist()) {//执行添加
            UserRole sup = new UserRole(sc.getTypeName(),Integer.valueOf(s));
            Integer rs = urm.insert(sup);
            if(rs!=1){
                r=false;
            }
        }
        if (r) {
            return new ResultData(true, 20000, "分配成功!", r, null);
        } else {
            return new ResultData(true, 20000, "分配失败!", r, null);
        }
    }
    @RequestMapping("/dels")
    public Object del(@RequestBody SearchCon sc) {
        List<UserRole> list=new ArrayList<UserRole>();
        if(sc.getNamelist().length!=0){
        for(String s:sc.getNamelist()){
            UserRole use=new UserRole();
            use.setUserId(sc.getTypeName());
            use.setRoleId(Integer.valueOf(s));
            list.add(use);
        }
        boolean r=urm.deletelist(list);
        if(r==true) {
            return new ResultData(true,20000,"移出成功!",r,null);
        }else {
            return new ResultData(true,20000,"移出失败!",r,null);
        }
      }else{
            QueryWrapper<UserRole> wra = new QueryWrapper<UserRole>();
            wra.eq(sc.getTypeName()!=null, "userId", sc.getTypeName());
            wra.eq(StringUtils.isnotNull(sc.getNamelist()[0]), "roleId",Integer.valueOf(sc.getNamelist()[0]));
            int r=urm.delete(wra);
            if(r==1){
            return new ResultData(true,20000,"移出成功!",true,null);}
            else{
                return new ResultData(true,20000,"移出失败!",false,null);
            }
    }
  }

}

